#!/usr/bin/perl

# ------------------------------------------------------------------------------
# --- Submits Rosetta job to NYU HPC
# --- Original by Andrea
# --- Re-written by C. Bergey and A. Watkins
# ------------------------------------------------------------------------------

use strict;
use warnings;

# http://search.cpan.org/~kwmak/PBS-Client/lib/PBS/Client.pm
use lib '/scratch/cmb433/pdb_interact/dependencies/PBS-Client-0.09/lib';

use PBS::Client;

my $pdb_code = shift;
my $chain1   = shift;
my $chain2   = shift;

defined ($chain2)
	or die "ERROR: Must pass PDB code and two chains as input.\n";

print STDERR "Submitting Rosetta job to NYU HPC for $pdb_code, ";
print STDERR "(first chain = $chain1, second chain = $chain2).\n";

# rosetta.gcc64 was built on HPC with command "make gcc64"

# Infer job name, something like
# 1A0H_A_B
my $job_name = $pdb_code . "_" . $chain1 . "_" . $chain2;

# Infer file name, something like
# pdb_partner_files/1A0H_A_B.pdb
my $pdb = "pdb_partner_files/" . $job_name . ".pdb";

my $rosetta_cmd = "./rosetta3.4/rosetta_source/bin/rosetta_scripts.linuxgccrelease ";
$rosetta_cmd .= " -s " . $pdb;
$rosetta_cmd .= " -use_input_sc -nstruct 20 -jd2:ntrials 2 ";
$rosetta_cmd .= "-database ./rosetta3.4/rosetta_database/ ";
$rosetta_cmd .= "-ex1 -ex2 ";
$rosetta_cmd .= "-parser:protocol rosetta_files/alascan.xml ";
$rosetta_cmd .= "-parser:view ";
$rosetta_cmd .= "> rosetta_output/" . $job_name . "_ala_out.txt\n";
	
# We now use direct job submission using PBS::Client

my $pbs_client = PBS::Client->new;
	
# Remove output PDB files (1A0H_A_D_0001.pdb, 1A0H_A_D_0002.pdb, etc.)
my $clean_up_cmd = "rm " . $job_name . "*";
	
my @emails = ['cmb433@nyu.edu'];
	
my @cmds = [$rosetta_cmd, $clean_up_cmd];

my $job = PBS::Client::Job->new(
	name => "rr_" . $job_name,
	wallt => "5:00:00", 
	cmd => \@cmds,
	maillist => @emails,
	mailopt => "bea",
);

my $retval = $pbs_client->qsub($job);

exit 0;
